package com.zmm.member.service.impl;

import com.zmm.common.constant.RedisTimeOutConstant;
import com.zmm.common.redis.key.RedisKey;
import com.zmm.common.redis.key.StringKey;
import com.zmm.member.service.SmsSendService;
import com.zmm.util.convert.StringUtil;
import com.zmm.util.redis.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author 900045
 * @description:
 * @name SmsSendServiceImpl
 * @date By 2021-08-05 18:48:48
 */
@Slf4j
@Service
public class SmsSendServiceImpl implements SmsSendService {

	@Resource
	private RedisUtil redisUtil;


	@Override
	public void insertVerificationSms(String phoneNum, Integer type, String ip) {
		// 有效期内返回同样的验证码
		RedisKey key = StringKey.MOBILE_CODE_KEY_.setSuffix(phoneNum, "_", type.toString());

		Object obj = redisUtil.get(key);
		String smsVerifyCode ;
		if (obj == null || StringUtils.isBlank(obj.toString())) {
			//生成6位随机数
			smsVerifyCode = StringUtil.getRandom(6);
			log.error("============当前手机号：{}，验证码：{}===============",phoneNum, smsVerifyCode);
			//验证码放入缓存(10分钟有效)
			redisUtil.setTime(key, smsVerifyCode, RedisTimeOutConstant.S_600, TimeUnit.SECONDS);
		} else {
			smsVerifyCode = obj.toString();
		}
	}
}
